package com.pony.iphone.utils.ding.pojo;

import com.pony.iphone.utils.ding.DingTalkUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 钉钉消息构建器
 * @author pony
 */
@Slf4j
public class DingTalkMessageBuilder {
    private final DingTalkUtil dingTalkUtil;
    private DingTalkMsgType msgType;
    private DingTalkAt at;
    private Object messageBody;
    
    public DingTalkMessageBuilder(DingTalkUtil dingTalkUtil) {
        this.dingTalkUtil = dingTalkUtil;
    }
    
    /**
     * 设置文本消息
     */
    public DingTalkMessageBuilder text(String content) {
        this.msgType = DingTalkMsgType.TEXT;
        DingTalkText text = new DingTalkText();
        text.setContent(content);
        this.messageBody = text;
        return this;
    }
    
    /**
     * 设置链接消息
     */
    public DingTalkMessageBuilder link(String title, String text, String messageUrl) {
        return link(title, text, messageUrl, null);
    }
    
    public DingTalkMessageBuilder link(String title, String text, String messageUrl, String picUrl) {
        this.msgType = DingTalkMsgType.LINK;
        DingTalkLink link = new DingTalkLink();
        link.setTitle(title)
            .setText(text)
            .setMessageUrl(messageUrl)
            .setPicUrl(picUrl);
        this.messageBody = link;
        return this;
    }
    
    /**
     * 设置Markdown消息
     */
    public DingTalkMessageBuilder markdown(String title, String text) {
        this.msgType = DingTalkMsgType.MARKDOWN;
        DingTalkMarkdown markdown = new DingTalkMarkdown();
        markdown.setTitle(title)
               .setText(text);
        this.messageBody = markdown;
        return this;
    }
    
    /**
     * 设置ActionCard消息
     */
    public DingTalkMessageBuilder actionCard(String title, String text, String singleTitle, String singleURL) {
        this.msgType = DingTalkMsgType.ACTION_CARD;
        DingTalkActionCard actionCard = new DingTalkActionCard();
        actionCard.setTitle(title)
                 .setText(text)
                 .setSingleTitle(singleTitle)
                 .setSingleURL(singleURL);
        this.messageBody = actionCard;
        return this;
    }
    
    /**
     * 设置带多个按钮的ActionCard
     */
    public DingTalkMessageBuilder actionCardWithButtons(String title, String text, List<DingTalkActionCard.Button> buttons) {
        this.msgType = DingTalkMsgType.ACTION_CARD;
        DingTalkActionCard actionCard = new DingTalkActionCard();
        actionCard.setTitle(title)
                 .setText(text)
                 .setBtns(buttons);
        this.messageBody = actionCard;
        return this;
    }
    
    /**
     * 设置FeedCard消息
     */
    public DingTalkMessageBuilder feedCard(List<DingTalkFeedCard.Link> links) {
        this.msgType = DingTalkMsgType.FEED_CARD;
        DingTalkFeedCard feedCard = new DingTalkFeedCard();
        feedCard.setLinks(links);
        this.messageBody = feedCard;
        return this;
    }
    
    /**
     * 设置@信息
     */
    public DingTalkMessageBuilder at(List<String> mobiles, List<String> userIds, Boolean isAtAll) {
        DingTalkAt atInfo = new DingTalkAt();
        atInfo.setAtMobiles(mobiles)
              .setAtUserIds(userIds)
              .setIsAtAll(isAtAll);
        this.at = atInfo;
        return this;
    }
    
    /**
     * 设置@所有人
     */
    public DingTalkMessageBuilder atAll() {
        DingTalkAt atInfo = new DingTalkAt();
        atInfo.setIsAtAll(true);
        this.at = atInfo;
        return this;
    }
    
    /**
     * 设置@指定手机号
     */
    public DingTalkMessageBuilder atMobiles(List<String> mobiles) {
        DingTalkAt atInfo = new DingTalkAt();
        atInfo.setAtMobiles(mobiles);
        this.at = atInfo;
        return this;
    }
    
    /**
     * 设置@指定用户ID
     */
    public DingTalkMessageBuilder atUserIds(List<String> userIds) {
        DingTalkAt atInfo = new DingTalkAt();
        atInfo.setAtUserIds(userIds);
        this.at = atInfo;
        return this;
    }
    
    /**
     * 发送消息
     */
    public boolean send() {
        return dingTalkUtil.sendMessage(this);
    }
    
    // Getter方法供DingTalkUtil使用
    public DingTalkMsgType getMsgType() {
        return msgType;
    }
    
    public DingTalkAt getAt() {
        return at;
    }
    
    public Object getMessageBody() {
        return messageBody;
    }
}